package com.merin.samples.andengine;

import org.anddev.andengine.engine.Engine;
import org.anddev.andengine.engine.camera.Camera;
import org.anddev.andengine.engine.options.EngineOptions;
import org.anddev.andengine.engine.options.EngineOptions.ScreenOrientation;
import org.anddev.andengine.engine.options.resolutionpolicy.RatioResolutionPolicy;
import org.anddev.andengine.entity.scene.Scene;
import org.anddev.andengine.entity.scene.Scene.IOnSceneTouchListener;
import org.anddev.andengine.entity.scene.background.ColorBackground;
import org.anddev.andengine.entity.sprite.AnimatedSprite;
import org.anddev.andengine.entity.sprite.Sprite;
import org.anddev.andengine.entity.util.FPSLogger;
import org.anddev.andengine.input.touch.TouchEvent;
import org.anddev.andengine.opengl.texture.Texture;
import org.anddev.andengine.opengl.texture.TextureOptions;
import org.anddev.andengine.opengl.texture.region.TextureRegion;
import org.anddev.andengine.opengl.texture.region.TextureRegionFactory;
import org.anddev.andengine.opengl.texture.region.TiledTextureRegion;
import org.anddev.andengine.ui.activity.BaseGameActivity;

import android.widget.Toast;

/**
 * @author Nicolas Gramlich
 * @since 11:54:51 - 03.04.2010
 */
public class SpriteRemoveExample extends BaseGameActivity implements IOnSceneTouchListener {
        // ===========================================================
        // Constants
        // ===========================================================

        private static final int CAMERA_WIDTH = 720;
        private static final int CAMERA_HEIGHT = 480;

        // ===========================================================
        // Fields
        // ===========================================================

        private Camera mCamera;
        private Texture mTexture;
        
        private TiledTextureRegion mFaceTextureRegion;
        private TextureRegion mBoxTextureRegion;
        private Sprite mBox;
        private Sprite mFaceToRemove;
        
        private AnimatedSprite mball;
        private TiledTextureRegion mBallTextureRegion;

        // ===========================================================
        // Constructors
        // ===========================================================

        // ===========================================================
        // Getter & Setter
        // ===========================================================

        // ===========================================================
        // Methods for/from SuperClass/Interfaces
        // ===========================================================

        @Override
        public Engine onLoadEngine() {
                Toast.makeText(this, "Touch the screen to safely remove the sprite.", Toast.LENGTH_SHORT).show();
                this.mCamera = new Camera(0, 0, CAMERA_WIDTH, CAMERA_HEIGHT);
                return new Engine(new EngineOptions(true, ScreenOrientation.LANDSCAPE, new RatioResolutionPolicy(CAMERA_WIDTH, CAMERA_HEIGHT), this.mCamera));
        }

        @Override
        public void onLoadResources() {
                this.mTexture = new Texture(128, 256, TextureOptions.BILINEAR_PREMULTIPLYALPHA);
                this.mBallTextureRegion = TextureRegionFactory.createTiledFromAsset(this.mTexture, this, "gfx/foot_ball.png", 0, 0, 2, 4);
                
                this.mEngine.getTextureManager().loadTexture(this.mTexture);
        }

        @Override
        public Scene onLoadScene() {
                this.mEngine.registerUpdateHandler(new FPSLogger());

                final Scene scene = new Scene(1);
                scene.setBackground(new ColorBackground(0.0f, 0.8274f, 0.0f));

                /* Calculate the coordinates for the face, so its centered on the camera. */
                final int centerX = (CAMERA_WIDTH - 64) / 2;
                final int centerY = (CAMERA_HEIGHT - 64) / 2;
                
                this.mball = new AnimatedSprite(centerX, centerY,this.mBallTextureRegion);

                scene.getLastChild().attachChild(this.mball);
                mball.animate(new long[] {200, 200 }, 1, 2, true);

                scene.setOnSceneTouchListener(this);

                return scene;
        }

        @Override
        public void onLoadComplete() {

        }

        @Override
        public boolean onSceneTouchEvent(final Scene pScene, final TouchEvent pSceneTouchEvent) {
                /* Removing entities can only be done safely on the UpdateThread.
                 * Doing it while updating/drawing can
                 * cause an exception with a suddenly missing entity.
                 * Alternatively, there is a possibility to run the TouchEvents on the UpdateThread by default, by doing:
                 * engineOptions.getTouchOptions().setRunOnUpdateThread(true);
                 * when creating the Engine in onLoadEngine(); */
                this.runOnUpdateThread(new Runnable() {
                        @Override
                        public void run() {
  
                        }		
                }
                );					
                return false;
        }

        // ===========================================================
        // Methods
        // ===========================================================

        // ===========================================================
        // Inner and Anonymous Classes
        // ===========================================================
}